<html><head> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> 
<meta name="keywords" content="wtf2l f2l oll pll trainer rubiks cube timer main page cubing speedcubing speedsolving fast fridrich method cfop"> 
<meta name="description" content="WTF2L cube algs"> 
<title>WTF2L Cube Algs</title> 
<title>F2L Trainer</title>
<style type="text/css">
<!-- 
#stat { position:absolute; left:5px; top:5px; } 
--> 
</style>
<script language="JavaScript" src="jquery-1.3.2.min.js"></script>
<script language="JavaScript"> 
var f2lfd = "nnnnunnnnnnnrrrrrrnnnffffffdddddddddnnnllllllnnnbbbbbb"; // F2L的面定义
var imgsize = 120; //题目图片大小
var excount = 18; //题目数量
// 246  240  230 F6 F0 E6
var imgurl = "http://cube.crider.co.uk/visualcube.php?size=" + imgsize + "&fmt=png&bg=EEE";
var scheme = "yrbwog"; // 白底
var alg_pos = 0; // 任意槽
var alg_slot = 5; // 无空槽
var view = "";
var ms = 0;
var state = 2;
var displayidx = 0;
var imgload = 0;
var algs = new Array(
"",
"(R U' U' R' U)2 y'(R' U' R)",
"(U R U' R' U') y'(R' U R)",
"y U'(L' U L U)y' (R U' R')",
"(R U R' U')(R U'U' R' U')(R U R')",
"(R U' R U)y(R U' R' F2)",
"y'(R' U' R U)(R' U' R)",
"(R U' R' U)(R U' R')",
"(R U'U')(R U R' U)(R U'U')R2",
"R2 y(R U R' U')y'(R' U R')",
"y' (R' U)(R U')(R' U R)",
"(R U R' U' )(R U R')",
"(R U R' U')2 (R U R')",
"d' z U'(R2 U)(R' U' R U)z'",
"y'(R' U2)(R U R' U')R",
"(R U' R') y' (R' U2 R)",
"F' L' U2 r U x'",
"y' (U2 R2' U2)(R U R' U R2)",
"(U2 R2' U2)(R' U' R U' R2)",
"R y (R U2 R' F')",
"U(R U'U')(R' U R U' )R' ",
"(R U'U')(R' U' R U)R'",
"U'(R U')(R' U2)(R U' R')",
"U' (R U R') d (R' U' R)",
"d (R' U R U')(R' U' R)",
"y' (R' U' R)",
"(R2 U)y(R U' R' F2)",
"y' (R U'U')R'2 U' R2 U' R'",
"y' (R' U)(R d' U')(R U R')",
"y' R2 y'(R' U' R U)y R2",
"U' (R U'U')(R' U2)(R U' R')",
"U R U' R'",
"U' (R U'U' R' U)(R U R')",
"d (R' U' R) d' (R U R')",
"x(U R' U' l)",
"R2 y (R U R' U')y' R2",
"d (R' U2)(R U'U')(R' U R)",
"(R U' R' U)(d R' U' R)",
"(R U R')",
"y' (R2 U')(F' U F)R2",
"(R' U2)(R2' U)(R2' U R)",
"U' (R U' R' U)(R U R')"
);
// 统计信息
var stat = {
	"totalcount":0,
	"totaltime":0,
	"counts":[0,0,0,0],
	"times":[0,0,0,0],
	"current":0
};
$(function(){
	for(var i = 0; i < algs.length; i++) {
	       algs[i] = normalalg(algs[i]);
	}
	$("#startbutton").click(start);
	$("#stopbutton").click(stop);
	$(document).keydown(function(evt){
		if(state != 2 && evt.keyCode == 32) {
			ondown();
		}
	});
	$(document).keyup(function(evt){
		if(state != 2 && evt.keyCode == 32) {
			onup();
		}
	});
	$("#subject").append("<img id=\"img\" src=\"\" width="+imgsize+" height="+imgsize+"/>");
	$("#subject img").load(imgonload);
	changesubject(0);
	// init subject
	$("#scheme").change(function(){
		scheme = this.value;
		changesubject(0);
	});
	$("#alg_pos").change(function(){
		alg_pos = this.value;
		changesubject(0);
	});
	$("#alg_slot").change(function(){
		alg_slot = this.value;
		changesubject(0);
	});
	displaytime();
});
function updatestat(ms)
{
	stat.totalcount++;
	stat.totaltime+=ms;
	stat.counts[stat.current]++;
	stat.times[stat.current]+=ms;
	var str = "";
	str += "次数:"+stat.totalcount+"<br/>";
	str += "平均:"+second2str(stat.totaltime/stat.totalcount)+"<br/>";
	var a = ["FR","BR","BL","FL"];
	for(var i = 0; i < 4; i++)
		str += a[i]+":"+second2str(stat.times[i]/stat.counts[i])+"/"+stat.counts[i]+"<br/>";
	$("#stat").html(str);
}
// 标准化公式,去括号空格
function normalalg(alg) {
	var re;
	re = /\(([udlrfbMESxyz'\s]*)\)\s*2/gi;
	alg = alg.replace(re, "$1$1");
	alg = alg.replace(/[\s\(\)]/g,"");
	alg = alg.replace(/(\w)(')(2)/g,"$1$3$2");
	return alg;
	//debug(alg+"<br/>");
}
function debug(str)
{
	document.getElementById("debug").innerHTML+=str;
}
function debugimg(img)
{
	document.getElementById("debug").innerHTML+="<img src=\""+img+"\"/>";
}
function getcube(index) {
	var cube = new Object();
	cube.sch = scheme;
	cube.view = view;
	cube.fd = f2lfd;
	if(alg_pos == 0) {
		pos = Math.floor(Math.random() * 4);
	} else {
		pos = alg_pos - 1;
	}
	if(alg_slot == 0) {
		slot = Math.floor(Math.random() * 4);
	} else {
		slot = alg_slot - 1;
	}
	alg = algs[index];
	// 随机选择颜色和U层方位
	r = Math.floor(Math.random() * 4);
	while(r--) alg += "y";
	r = Math.floor(Math.random() * 4);
	while(r--) alg = "U" + alg;

	if(pos == 0) {
		cube.r = "y30x-34";
	} else if(pos == 1) {
		alg = "y" + alg;
		cube.r = "y30x-34";
	} else if(pos == 2) {
		alg = "y2" + alg;
		cube.r = "y-30x-34";
	} else if(pos == 3) {
		alg = "y'" + alg;
		cube.r = "y30x-34";
	}
	// 槽位受到旋转影响
	if(slot != pos && slot < 4) {
		slot = (slot - pos + 4) % 4;
		var a = cube.fd.split("");
		if(slot == 0) {
			a[12]=a[15]=a[23]=a[26] = 'n';
		} else if(slot == 1) {
			a[14]=a[17]=a[48]=a[51] = 'n';
		} else if(slot == 2) {
			a[39]=a[42]=a[50]=a[53] = 'n';
		} else if(slot == 3) {
			a[21]=a[24]=a[41]=a[44] = 'n';
		}
		cube.fd = a.join("");
	}
	stat.current = pos;
	cube['case'] = alg;
	//cube.r = "";
	return cube2str(cube);
}
function cube2str(cube) {
	var s = "";
	for(key in cube) {
		s += "&"+key+"="+cube[key];
	}
	return s;
}
function changesubject(r) {
	imgload = 0;
	$("#subject img").each(function(i){
		var s;
		if(r == undefined)
			r = Math.floor(Math.random() * (algs.length - 1)) + 1;
		s = getcube(r);
		this.src = imgurl + s;
	});
}
function displaytime()
{
   setTimeout("displaytime();", 10);
   if (state == 1)
   {
      now = new Date();
      ms = now.getTime() - then.getTime();
      displayidx++;
      displayidx %= 6;
      if(displayidx == 0)
      drawtime(ms);
   }
}
function second2str(diff)
{
	if(isNaN(diff)) return "--:--.--";
	totalsec=Math.floor(diff/1000);
	sec=totalsec%60;
	if(sec<10)
	{
		secdis="0"+sec;
	}
	else
	{
		secdis=sec;
	}
	min=(totalsec-sec)/60;

	if (min<10)
	{
		mindis="0"+min;
	}
	else
	{
		mindis=min;
	}

	subsec=Math.floor((diff%1000)/10);

	if (subsec<10)
	{
		subsecdis="0"+subsec;
	}
	else 
	{
		subsecdis=subsec;
	}
	nowSeconds=mindis+":"+secdis+"."+subsecdis;
	return nowSeconds;
}
function drawtime(diff)
{	
	var nowSeconds = second2str(diff)
	document.getElementById("watch").innerHTML=nowSeconds;
}
function start()
{
	if (state == 2)
	{
		$("#startbutton").attr("disabled","disabled");
		$("#stopbutton").removeAttr("disabled");
		$("#startbutton").val("按空格键开始");
		state = 0;
		imgload = 0;
	}
}
function stop()
{
	if (state == 0 || state == 1)
	{
		$("#stopbutton").attr("disabled","disabled");
		$("#startbutton").removeAttr("disabled").val("Begin");
		state = 2;
	}
}
var keypressed = 0;
var keyrepeat = 0;
function ondown()
{
	if (state == 1) // 计时中, 按下停止计时
	{
		keypressed = 1;
		state = 0;
		imgload = 0;
		now = new Date();
		ms = now.getTime() - then.getTime();
		drawtime(ms);
		updatestat(ms);
		$("#startbutton").val("按空格键开始");
	}
	else if (state == 0) // 停止状态, 按下准备
	{
		// 准备
		if (imgload == 1) 
		{
			// nothing
		}
		else if(!keyrepeat)
		{
			// 换图片
			$("#startbutton").val("Waiting...");
			changesubject();
			keyrepeat = 1;
		}
	}
}
function onup()
{
	if(keypressed == 1 && state == 0) // 停止时按下,在此释放
	{
		keypressed = 0;
	}
	// ready
	else if (state == 0 && imgload == 1)
	{
		state = 1; //计时开始
		then = new Date();
		$("#startbutton").val("Go");
		//then.setTime(then.getTime() - ms);
	}
	keyrepeat = 0;
}
function imgonload() {
	if(state == 0) {
		imgload = 1; 
		drawtime(0);
		$("#startbutton").val("Ready");
	}
}
</script> 
</head>
<body>
<div id="stat">统计信息</div>
<center> 
<div id="debug"></div>
<h2>F2L Trainer</h2> 
</center> 
<center> 
<div id="subject"></div>
<form name="stpw"> 
<div id="watch" style="filter:shadraw(x=1,y=1,color=black);position:relative;width: 359px; height: 80px ; font-size:48pt; font-family:Arial; font-weight:bold; color:navy; text-align:center" >00:00.00</div> 
<input size="150" value="Begin"  id="startbutton" type="BUTTON"> 
<input size="150" value="End"  id="stopbutton" disabled="disabled" type="BUTTON"> 
<p></p> 
底色:<select id="scheme" size="1" >
	<option value="yrbwog" selected="selected">白底</option> 
	<option value="wobyrg">黄底</option> 
	<option value="gwrbyo">蓝底</option> 
	<option value="bwogyr">绿底</option> 
	<option value="obwrgy">红底</option> 
	<option value="rgwoby">橙底</option> 
</select>
方位:<select id="alg_pos" size="1" >
	 <option value="0" selected="selected">任意</option> 
	 <option value="1">FR</option> 
	 <option value="2">BR</option> 
	 <option value="3">BL</option> 
	 <option value="4">FL</option> 
</select>
空槽:<select id="alg_slot" size="1" > 
	 <option value="5" selected="selected">无</option> 
	 <option value="0">任意</option> 
	 <option value="1">FR</option> 
	 <option value="2">BR</option> 
	 <option value="3">BL</option> 
	 <option value="4">FL</option> 
</select><br/>
</form> 
<br> 
<p>Images generated by <a href="http://cube.crider.co.uk/visualcube.php">VisualCube</a></p> 
<p><a href="mailto:oyd_admin@163.com">Email me</a> for comments or suggestions</p> 
</center> 
</body></html> 
